//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "adminActions\fn_repair.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_params","_caller_name","_caller_uid","_vehicle_varname","_action","_authorized","_vehicle"];
PARAMS_3(_params,_caller_name,_caller_uid);

_authorized = _params select 0;
_action = _params select 1;
_vehicle = _params select 2;
_vehicle_varname = _params select 3;
_player_name = _params select 4;

_fnc_repair = {
	private ["_vehicle","_flipped","_vUp","_l","_winkel","_helper","_empty_spot"];
	_vehicle = _this;
	_vehicle setDamage 0;
	_vehicle setFuel 1;
	_flipped = false;
	_vUp = vectorUp _vehicle;
	if ((_vUp select 2) < 0) then {
		_flipped = true;
	} else {
		_l=sqrt((_vUp select 0)^2+(_vUp select 1)^2);
		if (_l != 0) then {
			_winkel = (_vUp select 2) atan2 _l;
			_flipped = (_winkel < 30);
		}
	};
	if (_flipped) then {
		_empty_spot = (position _vehicle) findEmptyPosition [0,20,typeof _vehicle];
		if (count _empty_spot <= 0) then {
			_empty_spot = (position _vehicle);
		};
		_vehicle allowDamage false;
		_helper = "Land_HelipadEmpty_F" createVehicleLocal (_empty_spot);
		_empty_spot set [2,(_empty_spot select 2) + 0.4];
		_vehicle setPos _empty_spot;
		_vehicle setVectorUp (vectorUp _helper);
		deleteVehicle _helper;
		_vehicle allowDamage true;
	};
};

if (isNil "_vehicle") exitWith {ERROR("Nil variable");};
if (isNull _vehicle) exitWith {ERROR("Null variable");};
if (isServer) then {
	TLOG_1("REPAIR",_this);
	if (local _vehicle) then {
		if (damage _vehicle < 0.7) then {
			_vehicle call _fnc_repair;
		};
	};
};
if (!isDedicated) then {
	if (local _vehicle) then {
		if (damage _vehicle < 0.7) then {
			_vehicle call _fnc_repair;
		};
	};
};

TRACE("End");
TMFPROFILERSTOP;
